<template>
  <div>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <el-form
        label-width="80px"
        :inline="true"
        size="default"
        :disabled="props.disabled"
      >
        <el-form-item label="一级分类">
          <el-select
            placeholder="请选择一级分类"
            clearable
            filterable
            @change="category1Change"
            v-model="(category1Id as number)"
          >
            <el-option
              v-for="category1 in category1List"
              :key="category1.id"
              :label="category1.name"
              :value="category1.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select
            placeholder="请选择二级分类"
            clearable
            filterable
            v-model="(category2Id as number)"
            @change="category2Change"
          >
            <el-option
              v-for="category2 in category2List"
              :key="category2.id"
              :label="category2.name"
              :value="category2.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="三级分类">
          <el-select
            placeholder="请选择三级分类"
            clearable
            filterable
            v-model="(category3Id as number)"
          >
            <el-option
              v-for="category3 in category3List"
              :key="category3.id"
              :label="category3.name"
              :value="category3.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "Category",
});
</script>

<script setup lang="ts">
import useCategoryStore from "@/stores/category";
import { storeToRefs } from "pinia";

// 获取props
const props = defineProps<{
  disabled: boolean;
}>();

// 获取category 仓库
const categroyStore = useCategoryStore();

// 发送一级列表请求数据
categroyStore.getCategory1List();

// 获取store 中的数据
const {
  category1List,
  category1Id,
  category2List,
  category2Id,
  category3List,
  category3Id,
} = storeToRefs(categroyStore);

// 一级分类被选中时事件
const category1Change = () => {
  // 重置二级三级列表数据
  category2List.value = [];
  category2Id.value = null;
  category3List.value = [];
  category3Id.value = null;
  // 获取二级列表
  if (!category1Id.value) return;
  categroyStore.getcategory2List();
};
// 二级分类被选中时事件
const category2Change = () => {
  // 重置三级列表数据
  category3List.value = [];
  category3Id.value = null;
  // 获取三级列表
  if (!category2Id.value) return;
  categroyStore.getcategory3List();
};
</script>

<style scoped lang="scss"></style>
